package hospital;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import movement.Location;


/**
 * A class for making an HospitalBoard, which contains a list with hospitals
 */
public class HospitalBoard {

	/**
	 * Variable registering the hospitals on this hospitalBoard
	 */
	private ArrayList<Hospital> hospitals;

	/**
	 * Initialize this new hospitalBoard 
	 * 
	 * @post	The list with hospitals is instantiated and empty
	 */
	public HospitalBoard() {
		setHospitals(new ArrayList<Hospital>());
	}

	/**
	 * Method for adding the given hospital to this HospitalBoard
	 * 
	 * @param h
	 * 		The hospital you want to add to this HospitalBoard
	 * 
	 * @post The given hospital is added to this HospitalBoard
	 * 
	 * @throws IllegalArgumentException, if the name is null or already occurs in this HospitalBoard
	 */
	public void addHospital(Hospital h){
		if(canAddToHospitalBoard(h))
			getHospitals().add(h);
		else
			throw new IllegalArgumentException("The given hospital can't be added to this hospitalboard, because it's null or the name already exists in this hospitalboard");
	} 
	
	/**
	 * Check the given hospital can be added to this HospitalBoard
	 * 
	 * @param h
	 *        The hospital you want to check for integrity
	 *        
	 * @return True when valid, False if not
	 */
	private boolean canAddToHospitalBoard(Hospital h) {
		if(h == null || h.getName() == null || h.getName() == "")
			return false;
		
		for(Hospital hh : getHospitals())
			if(hh.getName().equalsIgnoreCase(h.getName()))
					return false;
		
		return true;
	} 

	/**
	 * A method to get a list of all hospitals
	 * 
	 * @return a list of hospitals that are closest to the given location as param
	 */
	private ArrayList<Hospital> getHospitals() {
		return hospitals;
	}

	/**
	 * Method for getting a list with the hospitals, ordered by distance to the given location 
	 * (closest hospitals on front in list)
	 * 
	 * @param 	location
	 * 			The location on which the list needs to be ordered
	 * 
	 * @return	The list of hospitals on this HospitalBoard, ordered by distance between the given location and the hospitals
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ArrayList<Hospital> getHospitals(final Location location) {
		ArrayList<Hospital> hospitalList;
		hospitalList = new ArrayList<Hospital>();
		hospitalList = getHospitals();

		Collections.sort(hospitalList, new Comparator() {
			public int compare(Object o1, Object o2) {
				Location s1 = ((Hospital)o1).getLocation();
				Location s2 = ((Hospital)o2).getLocation();
				return s1.compareTo(s2, location);
			}
		});
		return hospitalList;
	}
	
	/**
	 * A method to get a list of all hospitals
	 * 
	 * @return a clone of the list of hospitals that are closest to the given location as param
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Hospital> getHospitalsClone() {
		return (ArrayList<Hospital>) hospitals.clone();
	}

	/**
	 * 
	 * @param 	hospitals
	 * 			The hospitals that needs to be set on this HospitalBoard
	 * 		
	 * @post	The hospitals are set to the given hospitals
	 */
	private void setHospitals(ArrayList<Hospital> hospitals) {
		this.hospitals = hospitals;
	}
}
